<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.prime.com.tr/ui"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">

    <body>
        <ui:composition template="../templates/default3.xhtml">
            <ui:define name="content">
                <h:form binding="#{salesOrderManagerBean.loadCancelSalesOrderPage}">
                    <p:panel header="Customer Details" id="customerDetailsPanel">
                        <table width="100%" border="0">
                            <tr>
                                <td width="200px">
                                    Company:
                                </td>
                                <td>
                                    <h:outputText id="company" value="#{salesOrderManagerBean.selectedCustomer.company}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Description:
                                </td>
                                <td>
                                    <h:outputText id="description" value="#{salesOrderManagerBean.selectedCustomer.description}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Annual Revenue:
                                </td>
                                <td>
                                    <h:outputText id="revenue" value="#{salesOrderManagerBean.selectedCustomer.revenue}">
                                        <f:convertNumber type="currency"/>
                                    </h:outputText>
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Number of Employees:
                                </td>
                                <td>
                                    <h:outputText id="numEmployee" value="#{salesOrderManagerBean.selectedCustomer.numEmployee}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Contact Person
                                </td>
                                <td>
                                    <h:outputText id="contactPerson" value="#{salesOrderManagerBean.selectedCustomer.firstName}, 
                                                  #{salesOrderManagerBean.selectedCustomer.lastName}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Designation:
                                </td>
                                <td>
                                    <h:outputText id="title" value="#{salesOrderManagerBean.selectedCustomer.title}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Contact Number:
                                </td>
                                <td>
                                    <h:outputText id="phone" value="#{salesOrderManagerBean.selectedCustomer.phone}" />
                                </td>
                            </tr>
                        </table>
                    </p:panel>
                    <p:panel header="Sales Orders" id="salesOrdersPanel">
                        <p:messages />
                        <p:dataTable id="salesOrders2" var="salesOrder" value="#{salesOrderManagerBean.salesOrders2}" 
                                     emptyMessage="No sales orders found with given criteria."
                                     selection="#{salesOrderManagerBean.selectedSalesOrder}"
                                     selectionMode="single"
                                     rowSelectListener="#{salesOrderManagerBean.onRowSelect}"
                                     onRowSelectUpdate="orderDetailsPanel,salesOrdersPanel" 
                                     rowUnselectListener="#{salesOrderManagerBean.onRowUnselect}"
                                     onRowUnselectUpdate="orderDetailsPanel,salesOrdersPanel">
                            <p:column headerText="PO ID" sortBy="#{salesOrder.job.purchaseOrder.id}">  
                                <h:outputText value="#{salesOrder.job.purchaseOrder.id}"/> 
                            </p:column> 
                            <p:column headerText="Total Amount" sortBy="#{salesOrder.job.purchaseOrder.totalAmount}">  
                                <h:outputText value="#{salesOrder.job.purchaseOrder.totalAmount}">  
                                    <f:convertNumber type="currency"/>
                                </h:outputText>
                            </p:column> 
                            <p:column headerText="Credit Rate (%)" sortBy="#{salesOrder.job.purchaseOrder.creditRate}">  
                                <h:outputText value="#{salesOrder.job.purchaseOrder.creditRate}">
                                </h:outputText>
                            </p:column> 
                            <p:column headerText="Status" sortBy="#{salesOrder.status}">  
                                <h:outputText value="#{salesOrder.status}">
                                </h:outputText>
                            </p:column> 
                        </p:dataTable>
                    </p:panel>
                    <p:panel header="Order Details" id="orderDetailsPanel">
                        <p:messages />
                        <p:dataTable id="purchaseItems" var="purchaseItem" value="#{salesOrderManagerBean.purchaseItems}" 
                                     emptyMessage="No purchase items found with given criteria."
                                     selection="#{salesOrderManagerBean.selectedPurchaseItem}"
                                     selectionMode="single"
                                     rowSelectListener="#{salesOrderManagerBean.onRowSelect}"
                                     rowUnselectListener="#{salesOrderManagerBean.onRowUnselect}">
                            <p:column headerText="Product" sortBy="#{purchaseItem.product.name}">  
                                <h:outputText value="#{purchaseItem.product.name}">
                                </h:outputText>
                            </p:column> 
                            <p:column headerText="Quantity" sortBy="#{purchaseItem.quantity}">  
                                <h:outputText value="#{purchaseItem.quantity}">
                                </h:outputText>
                            </p:column> 
                            <p:column headerText="Total Amount" sortBy="#{purchaseItem.subTotal}">  
                                <h:outputText value="#{purchaseItem.subTotal}">  
                                    <f:convertNumber type="currency"/>
                                </h:outputText>
                            </p:column> 
                            <p:column headerText="Outstanding Amount" sortBy="#{purchaseItem.outstandingAmount}">  
                                <h:outputText value="#{purchaseItem.outstandingAmount}">
                                    <f:convertNumber type="currency"/>
                                </h:outputText>
                            </p:column> 
                            <p:column headerText="Status" sortBy="#{purchaseItem.status}">  
                                <h:outputText value="#{purchaseItem.status}">
                                </h:outputText>
                            </p:column> 
                        </p:dataTable>
                        <h:selectOneMenu id="selectOneReason" value="#{salesOrderManagerBean.cancelReason}"
                                         title="Reason for cancellation of order or order item.">
                            <f:selectItem itemLabel="- Select Reason -" itemValue="" />
                            <f:selectItem itemLabel="Unable to fulfill" itemValue="UTF" />
                            <f:selectItem itemLabel="Customer's decision" itemValue="CD" />
                        </h:selectOneMenu>
                        <p:commandButton id="btnCancelPurchaseItem" value="Cancel Single Item" 
                                         title="Cancel the selected sales order item."
                                         onclick="confirmation2.show()"/>
                        <p:commandButton id="btnCancelSalesOrder" value="Cancel All Items" 
                                         title="Cancel the whole sales order."
                                         onclick="confirmation.show()"/>
                    </p:panel>
                    <p:confirmDialog  width="400" message="" header="Cancelling a sales order" severity="alert" widgetVar="confirmation">
                        <f:facet name="message">
                            <h:outputText value="Cancelling this sales order will stop any related processes for the order."/><br/>
                            <h:outputText value="Are you sure you want to continue?"/>
                        </f:facet>    
                        <p:commandButton value="Yes" update="salesOrdersPanel" onclick="confirmation.hide()"
                                         actionListener="#{salesOrderManagerBean.cancelSalesOrder}"/>
                        <p:commandButton value="No" update="salesOrdersPanel" onclick="confirmation.hide()" type="button" />
                    </p:confirmDialog>
                    <p:confirmDialog  width="400" message="" header="Cancelling a purchase item" severity="alert" widgetVar="confirmation2">
                        <f:facet name="message">
                            <h:outputText value="Cancelling this purchase item will stop any related processes for this item."/><br/>
                            <h:outputText value="Are you sure you want to continue?"/>
                        </f:facet>    
                        <p:commandButton value="Yes" update="orderDetailsPanel" onclick="confirmation2.hide()"
                                         actionListener="#{salesOrderManagerBean.cancelPurchaseItem}"/>
                        <p:commandButton value="No" update="orderDetailsPanel" onclick="confirmation2.hide()" type="button" />
                    </p:confirmDialog>
                </h:form>
                <p:ajaxStatus onstart="statusDialog.show();" onsuccess="statusDialog.hide();"/>  
                <p:dialog modal="true" widgetVar="statusDialog" header="Please wait..."   
                          draggable="false" closable="false" resizable="false">  
                    <p:graphicImage value="../resources/images/ajaxloadingbar.gif" />  
                </p:dialog>
            </ui:define>
        </ui:composition>
    </body>
</html>
